<template>
  <div class="app-container" ref="container">
    <!-- 固定的搜索和工具栏容器 -->
    <div class="fixed-header">
      <!-- 搜索栏 -->
      <div class="search-bar">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="小说编号">
            <el-input v-model="formInline.pid" placeholder="小说编号"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleQuery">查 询</el-button>
            <el-button @click="resetQuery">重 置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 工具栏 -->
      <div class="toolbar-right">
        <el-button size="small" @click="handleRefresh">
          <i class="el-icon-refresh"></i> 刷新
        </el-button>
      </div>
    </div>

    <!-- 表格内容区域 -->
    <div class="table-container">
      <!-- 表格和分页容器 -->
      <div class="content-container">
        <el-table
            ref="table"
            v-loading="loading"
            :data="novelList"
            border
            style="width: 100%"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="id" label="ID" width="80"></el-table-column>
          <el-table-column prop="comment" label="评论内容" min-width="200">
            <template slot-scope="scope">
              <el-link type="primary" @click="handleView(scope.row)">{{ scope.row.comment }}</el-link>
            </template>
          </el-table-column>
          <el-table-column prop="commenttime" label="评论时间" width="100"></el-table-column>
          <el-table-column label="操作" width="200" fixed="right">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="text"
                  @click="handleDelete(scope.row)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-container">
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryParams.pageNo"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="queryParams.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'NovelList',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      selectedIds: [],
      // 总条数
      total: 0,
      // 小说表格数据
      novelList: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
      },
      formInline: {
        pid: ''
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询小说列表 */
    getList() {
      this.formInline.pid = this.$route.query.id;
      this.axios.post(`http://localhost:9999/api-novel/score/findcomment?pageNo=${this.queryParams.pageNo}&pageSize=${this.queryParams.pageSize}`, this.formInline).then(res => {
        this.novelList = res.data.data.list;
        this.total = res.data.data.total;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.queryParams.pid = this.formInline.pid
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      // 重置查询表单
      this.formInline = {
        pid: ''
      }
      // 重置分页参数
      this.queryParams = {
        pageNo: 1,
        pageSize: 10
      }
      // 清空选中项（如果有使用表格多选的话）
      if (this.$refs.table) {
        this.$refs.table.clearSelection()
      }
      // 重新加载数据
      this.getList()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除该评论?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用删除接口
        this.axios.post(`http://localhost:9999/api-novel/score/delete?id=${row.id}`)
            .then(res => {
              if (res.data) {
                this.$message.success('删除成功')
                this.getList() // 刷新列表
              } else {
                this.$message.error(res.data.msg || '删除失败')
              }
            })
            .catch(error => {
              console.error('删除失败:', error)
              this.$message.error('删除失败，服务器错误')
            })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    /** 刷新按钮操作 */
    handleRefresh() {
      this.getList()
    },
    /** 查看小说详情 */
    handleView(row) {
      this.$message.success('查看小说：' + row.title)
    },
    // 分页大小改变
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getList()
    },
    // 当前页改变
    handleCurrentChange(val) {
      this.queryParams.pageNo = val
      this.getList()
    },
  }
}
</script>

<style scoped>
.app-container {
  padding: 0;
  background-color: #fff;
  width: 100%;
  overflow: hidden;
  position: relative;
}

.fixed-header {
  position: fixed;
  top: 50px; /* 与顶部导航栏对齐 */
  left: 210px; /* 与侧边栏对齐 */
  right: 0;
  background-color: #fff;
  z-index: 999;
  padding: 15px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 1px 4px rgba(0, 21, 41, .08);
}

.search-bar {
  flex: 1;
}

.toolbar-right {
  margin-left: 20px;
}

.table-container {
  margin-top: 70px; /* 为固定头部留出空间 */
  padding: 0 20px;
}

.content-container {
  background-color: #fff;
  border-radius: 4px;
  width: 100%;
  overflow-x: hidden;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
  padding: 10px 0;
}

.el-table {
  margin-bottom: 20px;
}

.el-link {
  font-size: 12px;
}

/* 添加全局样式，隐藏滚动条但保持滚动功能 */
::-webkit-scrollbar {
  display: none;
}

/* 调整表单项的间距 */
.demo-form-inline .el-form-item {
  margin-bottom: 0;
  margin-right: 20px;
}

.demo-form-inline .el-form-item:last-child {
  margin-right: 0;
}
</style>